{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Title: #Minimum Common Value"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Difficulty: #Easy"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Category Title: #Algorithms"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Tag Slug: #array #hash-table #two-pointers #binary-search"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Name Translated: #数组 #哈希表 #双指针 #二分查找"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Solution Name: getCommon"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Translated Title: #最小公共值"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Translated Content:\n",
    "<p>给你两个整数数组&nbsp;<code>nums1</code> 和&nbsp;<code>nums2</code>&nbsp;，它们已经按非降序排序，请你返回两个数组的 <strong>最小公共整数</strong>&nbsp;。如果两个数组&nbsp;<code>nums1</code> 和&nbsp;<code>nums2</code>&nbsp;没有公共整数，请你返回&nbsp;<code>-1</code>&nbsp;。</p>\n",
    "\n",
    "<p>如果一个整数在两个数组中都 <strong>至少出现一次</strong>&nbsp;，那么这个整数是数组&nbsp;<code>nums1</code> 和&nbsp;<code>nums2</code>&nbsp;<strong>公共</strong>&nbsp;的。</p>\n",
    "\n",
    "<p>&nbsp;</p>\n",
    "\n",
    "<p><strong>示例 1：</strong></p>\n",
    "\n",
    "<pre><b>输入：</b>nums1 = [1,2,3], nums2 = [2,4]\n",
    "<b>输出：</b>2\n",
    "<b>解释：</b>两个数组的最小公共元素是 2 ，所以我们返回 2 。\n",
    "</pre>\n",
    "\n",
    "<p><strong>示例 2：</strong></p>\n",
    "\n",
    "<pre><b>输入：</b>nums1 = [1,2,3,6], nums2 = [2,3,4,5]\n",
    "<b>输出：</b>2\n",
    "<b>解释：</b>两个数组中的公共元素是 2 和 3 ，2 是较小值，所以返回 2 。\n",
    "</pre>\n",
    "\n",
    "<p>&nbsp;</p>\n",
    "\n",
    "<p><strong>提示：</strong></p>\n",
    "\n",
    "<ul>\n",
    "\t<li><code>1 &lt;= nums1.length, nums2.length &lt;= 10<sup>5</sup></code></li>\n",
    "\t<li><code>1 &lt;= nums1[i], nums2[j] &lt;= 10<sup>9</sup></code></li>\n",
    "\t<li><code>nums1</code> 和&nbsp;<code>nums2</code>&nbsp;都是 <strong>非降序</strong>&nbsp;的。</li>\n",
    "</ul>\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Description: [minimum-common-value](https://leetcode.cn/problems/minimum-common-value/description/)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Solutions: [minimum-common-value](https://leetcode.cn/problems/minimum-common-value/solutions/)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "test_cases = ['[1,2,3]\\n[2,4]', '[1,2,3,6]\\n[2,3,4,5]']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def getCommon(self, nums1: List[int], nums2: List[int]) -> int:\n",
    "        nums1.sort()\n",
    "        nums2.sort()\n",
    "        if nums1[0] > nums2[-1] or nums2[0] > nums1[-1]:\n",
    "            return -1\n",
    "        n1, n2 = len(nums1), len(nums2)\n",
    "        while n1 > 0 and n2 > 0:\n",
    "            if nums1[0] == nums2[0]:\n",
    "                return nums1[0]\n",
    "            elif nums1[0] > nums2[0]:\n",
    "                nums2.pop(0)\n",
    "                n2 -= 1\n",
    "            else:\n",
    "                nums1.pop(0)\n",
    "                n1 -= 1\n",
    "\n",
    "        return -1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def getCommon(self, nums1: List[int], nums2: List[int]) -> int:\n",
    "        cur1 = cur2 = 0\n",
    "        l1, l2 = len(nums1), len(nums2)\n",
    "        while cur1 < l1 and cur2 < l2:\n",
    "            if nums1[cur1] < nums2[cur2]:\n",
    "                cur1 += 1\n",
    "            elif nums1[cur1] > nums2[cur2]:\n",
    "                cur2 += 1\n",
    "            else:\n",
    "                return nums1[cur1]\n",
    "        return -1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def getCommon(self, nums1: List[int], nums2: List[int]) -> int:\n",
    "        pointer1 = 0\n",
    "        pointer2 = 0\n",
    "\n",
    "        len1 = len(nums1)\n",
    "        len2 = len(nums2)\n",
    "\n",
    "        while(pointer1 < len1 and pointer2 < len2):\n",
    "            if nums1[pointer1] == nums2[pointer2]:\n",
    "                return nums1[pointer1]\n",
    "            elif nums1[pointer1] < nums2[pointer2]:    \n",
    "                pointer1+=1\n",
    "            elif nums1[pointer1] > nums2[pointer2]:    \n",
    "                pointer2+=1\n",
    "\n",
    "        return -1         \n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def getCommon(self, nums1: List[int], nums2: List[int]) -> int:\n",
    "        if len(nums1) < len(nums2):\n",
    "            return self.getCommon(nums2, nums1)\n",
    "        n1, n2 = len(nums1), len(nums2)\n",
    "        ans = 0\n",
    "        p, q = 0, 0\n",
    "        while p < n1 and q < n2:\n",
    "            if nums1[p] == nums2[q]:\n",
    "                return nums1[p]\n",
    "            elif nums1[p] < nums2[q]:\n",
    "                p += 1\n",
    "            else:\n",
    "                q += 1\n",
    "        return -1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def getCommon(self, nums1: List[int], nums2: List[int]) -> int:\n",
    "        if len(nums1) < len(nums2):\n",
    "            return self.getCommon(nums2, nums1)\n",
    "        n1, n2 = len(nums1), len(nums2)\n",
    "        ans = 0\n",
    "        p, q = 0, 0\n",
    "        while p < n1 and q < n2:\n",
    "            while p < n1 and q < n2 and nums1[p] < nums2[q]:\n",
    "                p += 1\n",
    "            while p < n1 and q < n2 and nums1[p] > nums2[q]:\n",
    "                q += 1\n",
    "            if p == n1 or q == n2:\n",
    "                return -1\n",
    "            if nums1[p] == nums2[q]:\n",
    "                return nums1[p]\n",
    "        return -1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def getCommon(self, nums1: List[int], nums2: List[int]) -> int:\n",
    "        l=0\n",
    "        len_=len(nums1)\n",
    "        for i in nums2:\n",
    "            while l<len_ and nums1[l]<i:\n",
    "                l+=1\n",
    "            if l<len_ and nums1[l]==i:\n",
    "                return i\n",
    "        return -1\n",
    "        \n",
    "        "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def getCommon(self, nums1: List[int], nums2: List[int]) -> int:\n",
    "        def binarySearch(i, j, target, nums):\n",
    "            while j - i > 3:\n",
    "                mid = (i+j) >> 1\n",
    "                if nums[mid] > target: j = mid\n",
    "                else: i = mid\n",
    "            for k in range(i, j+1):\n",
    "                if nums[k] == target:\n",
    "                    return nums[k]\n",
    "            return -1\n",
    "        \n",
    "        for x in nums1:\n",
    "            val = binarySearch(0, len(nums2)-1, x, nums2)\n",
    "            if val != -1:\n",
    "                return val\n",
    "        return -1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def getCommon(self, nums1: List[int], nums2: List[int]) -> int:\n",
    "        l=r=0\n",
    "        len1,len2=len(nums1),len(nums2)\n",
    "        ans=-1\n",
    "        while l<len1 and r<len2:\n",
    "            if nums1[l]==nums2[r]:\n",
    "                return nums1[l]\n",
    "            elif nums1[l]>nums2[r]:\n",
    "                r+=1\n",
    "            else:\n",
    "                l+=1\n",
    "        return ans\n",
    "        \n",
    "        "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def getCommon(self, nums1: List[int], nums2: List[int]) -> int:\n",
    "        n,m = len(nums1),len(nums2)\n",
    "        i,j = 0,0\n",
    "\n",
    "        while i < n and j < m:\n",
    "            if nums1[i] == nums2[j]:\n",
    "                return nums2[j]\n",
    "            elif nums1[i] < nums2[j]:\n",
    "                i += 1\n",
    "            else:\n",
    "                j += 1\n",
    "\n",
    "        return -1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def getCommon(self, nums1: List[int], nums2: List[int]) -> int:\n",
    "        s = set(nums2)\n",
    "        for n in nums1:\n",
    "            if n in s:\n",
    "                return n\n",
    "        return -1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def getCommon(self, nums1: List[int], nums2: List[int]) -> int:\n",
    "        \n",
    "        if (nums2[0] > nums1[-1]) or (nums2[-1] < nums1[0]):\n",
    "            return -1\n",
    "        a = nums1.pop(0)\n",
    "        b = nums2.pop(0)\n",
    "        if a == b:\n",
    "            return a\n",
    "        while len(nums1) or len(nums2):\n",
    "            print(a, b)\n",
    "            if a == b:\n",
    "                return a\n",
    "            if a > b:\n",
    "                b = nums2.pop(0)\n",
    "            else:\n",
    "                a = nums1.pop(0)\n",
    "        \n",
    "        if a == b:\n",
    "            return a\n",
    "        else: \n",
    "            return -1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def getCommon(self, nums1: List[int], nums2: List[int]) -> int:\n",
    "        s2 = set(nums2)\n",
    "        for num in nums1:\n",
    "            if num in s2: return num\n",
    "        return -1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def getCommon(self, nums1: List[int], nums2: List[int]) -> int:\n",
    "        s2 = set(nums2)\n",
    "        ans = inf\n",
    "        for num1 in nums1:\n",
    "            if num1 in s2:\n",
    "                ans = min(ans, num1)\n",
    "        \n",
    "        return ans if ans != inf else -1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def getCommon(self, nums1: List[int], nums2: List[int]) -> int:\n",
    "        s = set(nums1)\n",
    "        ans = inf\n",
    "        for x in nums2:\n",
    "            if x in s and x < ans:\n",
    "                ans = x\n",
    "        return ans if ans != inf else -1\n",
    "                "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def getCommon(self, nums1: List[int], nums2: List[int]) -> int:\n",
    "        st = set(nums2)\n",
    "        for i in range(len(nums1)):\n",
    "            if nums1[i] in st:\n",
    "                return nums1[i]\n",
    "        return -1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def getCommon(self, nums1: List[int], nums2: List[int]) -> int:\n",
    "        setNums2 = set(nums2)\n",
    "        for a in sorted(nums1):\n",
    "            if a in setNums2:\n",
    "                return a\n",
    "        return -1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def getCommon(self, nums1: List[int], nums2: List[int]) -> int:\n",
    "        nums=sorted(list(set(nums1))+list(set(nums2)))\n",
    "        begin=1\n",
    "        end=len(nums)\n",
    "        while begin<end:\n",
    "            if nums[begin]==nums[begin-1]:\n",
    "                return nums[begin]\n",
    "                break\n",
    "            begin+=1\n",
    "        return -1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def getCommon(self, nums1: List[int], nums2: List[int]) -> int:\n",
    "        st = set(nums2)\n",
    "        for num in nums1:\n",
    "            if num in st:\n",
    "                return num\n",
    "        return -1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def getCommon(self, nums1: List[int], nums2: List[int]) -> int:\n",
    "        #  输入：nums1 = [1,2,3,6], nums2 = [2,3,4,5]\n",
    "        # 输出：2\n",
    "        # 解释：两个数组中的公共元素是 2 和 3 ，2 是较小值，所以返回 2 。\n",
    "        ans = 1e11\n",
    "        pool = set(nums1)\n",
    "\n",
    "        for num in nums2:\n",
    "            if num in pool:\n",
    "                ans = min(ans, num)\n",
    "        return ans if ans != 1e11 else -1\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def getCommon(self, nums1: List[int], nums2: List[int]) -> int:\n",
    "        mp = set(nums2)\n",
    "        nums1.sort()\n",
    "        for x in nums1:\n",
    "            if x in mp:\n",
    "                return x \n",
    "        return -1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def getCommon(self, nums1: List[int], nums2: List[int]) -> int:\n",
    "        nums1.sort()\n",
    "        dct = set(nums2)\n",
    "        for num in nums1:\n",
    "            if num in dct:\n",
    "                return num\n",
    "        return -1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def getCommon(self, nums1: List[int], nums2: List[int]) -> int:\n",
    "        d = {}\n",
    "        for n in nums1:\n",
    "            d[n]=1\n",
    "        \n",
    "        for n in nums2:\n",
    "            if( d.get(n,0) > 0 ):\n",
    "                return n\n",
    "        return -1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def getCommon(self, nums1: List[int], nums2: List[int]) -> int:\n",
    "        n1 = len(nums1)\n",
    "        s = set(nums2)\n",
    "        for i in range(n1):\n",
    "            if nums1[i] in s:\n",
    "                return nums1[i]\n",
    "        return -1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def getCommon(self, nums1: List[int], nums2: List[int]) -> int:\n",
    "        set1 = nums1\n",
    "        set2 = set(nums2)\n",
    "        minVal = -1\n",
    "        for i in set1:\n",
    "            if i in set2:\n",
    "                minVal = i\n",
    "                break\n",
    "        return minVal"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def getCommon(self, nums1: List[int], nums2: List[int]) -> int:\n",
    "        INF = 10 ** 9 + 1\n",
    "\n",
    "        vis = set(nums1)\n",
    "        res = INF\n",
    "        for x in nums2:\n",
    "            if x in vis:\n",
    "                res = min(res, x)\n",
    "        \n",
    "        return res if res != INF else -1\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def getCommon(self, nums1: List[int], nums2: List[int]) -> int:\n",
    "        A=set(nums1)\n",
    "        nums2.sort()\n",
    "        for i in nums2:\n",
    "            if i in A:\n",
    "                return i\n",
    "        return -1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def getCommon(self, nums1: List[int], nums2: List[int]) -> int:\n",
    "        res = -1\n",
    "        st = set(nums1)\n",
    "        for num in nums2:\n",
    "            if num in st:\n",
    "                if res == -1 or res > num:\n",
    "                    res = num\n",
    "        \n",
    "        return res\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def getCommon(self, nums1: List[int], nums2: List[int]) -> int:\n",
    "        if len(nums1) < len(nums2) :\n",
    "            nums1 , nums2 = nums2 , nums1\n",
    "        # s1 = set(nums1)\n",
    "        s2 = set(nums2)\n",
    "        for i in nums1 :\n",
    "            if i in s2 : \n",
    "                return i \n",
    "        \n",
    "        return -1\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def getCommon(self, nums1: List[int], nums2: List[int]) -> int:\n",
    "        tmp = {}\n",
    "        for i in nums1:\n",
    "            tmp[i] = 1\n",
    "        for j in nums2:\n",
    "            if j in tmp:\n",
    "                return j\n",
    "        return -1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def getCommon(self, nums1: List[int], nums2: List[int]) -> int:\n",
    "        '''\n",
    "        简单易想但超时，考虑到是有序数组，应有更好方法\n",
    "        for i in nums1:\n",
    "            if i in nums2:\n",
    "                return i\n",
    "        return -1\n",
    "        '''\n",
    "        nums=set(nums1)&set(nums2)\n",
    "        if len(nums)!=0:\n",
    "            return min(nums)\n",
    "        else:\n",
    "            return -1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def getCommon(self, nums1: List[int], nums2: List[int]) -> int:\n",
    "        d = {}\n",
    "        for n in nums1:\n",
    "            d[n] = 1\n",
    "        \n",
    "        for n in nums2:\n",
    "            if n in d:\n",
    "                return n\n",
    "        \n",
    "        return -1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def getCommon(self, nums1: List[int], nums2: List[int]) -> int:\n",
    "        cnt = Counter(nums1)\n",
    "        # print(cnt)\n",
    "        ans = inf\n",
    "        for n in nums2:\n",
    "            if cnt[n] >= 1:\n",
    "                if n < ans:\n",
    "                    ans = n\n",
    "        return ans if ans != inf else -1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def getCommon(self, nums1: List[int], nums2: List[int]) -> int:\n",
    "        return min(set(nums1) & set(nums2) , default = -1) "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def getCommon(self, nums1: List[int], nums2: List[int]) -> int: \n",
    "        nums1 = list(set(nums1))\n",
    "        nums2 = list(set(nums2))\n",
    "        nums1.sort()\n",
    "        nums2.sort()\n",
    "        i, j = 0, 0\n",
    "        l1, l2 = len(nums1), len(nums2)\n",
    "        \n",
    "        try:\n",
    "            while True:\n",
    "                if nums1[i] < nums2[j]:\n",
    "                    i += 1\n",
    "                elif nums1[i] > nums2[j]:\n",
    "                    j += 1\n",
    "                else:\n",
    "                    return nums1[i]\n",
    "        except IndexError:\n",
    "            return -1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def getCommon(self, nums1: List[int], nums2: List[int]) -> int:\n",
    "        new_nums = list(set(nums1)) + list(set(nums2))\n",
    "        new_nums.sort()\n",
    "\n",
    "        for i in range(len(new_nums) - 1):\n",
    "            if new_nums[i] == new_nums[i + 1]:\n",
    "                return new_nums[i]\n",
    "\n",
    "        return -1\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def getCommon(self, nums1: List[int], nums2: List[int]) -> int:\n",
    "        new_nums = list(set(nums1)) + list(set(nums2))\n",
    "        new_nums.sort()\n",
    "\n",
    "        for i in range(len(new_nums) - 1):\n",
    "            if new_nums[i] == new_nums[i + 1]:\n",
    "                return new_nums[i]\n",
    "\n",
    "        return -1\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def getCommon(self, nums1: List[int], nums2: List[int]) -> int:\n",
    "        a = list(set(nums1))\n",
    "        b = list(set(nums2))\n",
    "        a.sort()\n",
    "        b.sort()\n",
    "\n",
    "        left1 = 0\n",
    "        left2 = 0\n",
    "        while left1 <= len(a) - 1 and left2 <= len(b) - 1:\n",
    "            if a[left1] == b[left2]:\n",
    "                return a[left1]\n",
    "                break\n",
    "            else:\n",
    "                if a[left1] > b[left2]:\n",
    "                    left2 += 1\n",
    "                else:\n",
    "                    left1 += 1\n",
    "\n",
    "        return -1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def getCommon(self, nums1: List[int], nums2: List[int]) -> int:\n",
    "        nums = list(set(nums1) & set(nums2))\n",
    "        nums.sort()\n",
    "        if len(nums) == 0:\n",
    "            return -1\n",
    "        return nums[0]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def getCommon(self, nums1: List[int], nums2: List[int]) -> int:\n",
    "        from collections import Counter\n",
    "        dic=Counter(nums1)\n",
    "        for k in nums2:\n",
    "            if k in dic:\n",
    "                return k\n",
    "\n",
    "        return -1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def getCommon(self, nums1: List[int], nums2: List[int]) -> int:\n",
    "        dict = {}\n",
    "        for num in nums1:\n",
    "            dict.setdefault(num)\n",
    "        for num in nums2:\n",
    "            if dict.get(num,0) != 0  :\n",
    "                return num\n",
    "        return -1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def getCommon(self, nums1: List[int], nums2: List[int]) -> int:\n",
    "        diction={}\n",
    "        answer=-1\n",
    "        for num in nums1:\n",
    "            if num not in diction.keys():\n",
    "                diction[num]=1\n",
    "        for num in nums2:\n",
    "            if num in diction.keys():\n",
    "                diction[num]=2\n",
    "        for num in list(diction.keys()):\n",
    "            if diction[num]==2:\n",
    "                answer=num\n",
    "                break\n",
    "        return answer"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def getCommon(self, nums1: List[int], nums2: List[int]) -> int:\n",
    "\n",
    "            return min(set(nums1) & set(nums2), default=-1)\n",
    "\n",
    "\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def getCommon(self, nums1: List[int], nums2: List[int]) -> int:\n",
    "        nums = list(set(nums1).intersection(set(nums2)))\n",
    "        if not nums:\n",
    "            return -1\n",
    "        return min(nums)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def getCommon(self, nums1: List[int], nums2: List[int]) -> int:\n",
    "        ls = list(set(nums1)&set(nums2))\n",
    "        if ls:\n",
    "            return min(ls)\n",
    "        else:\n",
    "            return -1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def getCommon(self, nums1: List[int], nums2: List[int]) -> int:\n",
    "        try:\n",
    "            return min(set(nums1)&set(nums2))\n",
    "        except:\n",
    "            return -1\n",
    "        "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def getCommon(self, nums1: List[int], nums2: List[int]) -> int:\n",
    "        if nums1[-1]<nums2[0] or nums2[-1]<nums1[0]:\n",
    "            return -1\n",
    "        nums1 = sorted(list(set(nums1)))\n",
    "        nums2 = set(nums2)\n",
    "        for i in nums1:\n",
    "            if i in nums2:\n",
    "                return i\n",
    "        return -1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def getCommon(self, nums1: List[int], nums2: List[int]) -> int:\n",
    "        if nums1[-1]<nums2[0] or nums2[-1]<nums1[0]:\n",
    "            return -1\n",
    "        nnums1 = sorted(list(set(nums1)))\n",
    "        nums2 = sorted(list(set(nums2)))\n",
    "        for i in nums1:\n",
    "            if i in nums2:\n",
    "                return i\n",
    "        return -1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\r\n",
    "    def getCommon(self, nums1, nums2) -> int:\r\n",
    "        n = set(nums1)&set(nums2)\r\n",
    "        return min(n) if n else -1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def getCommon(self, nums1: List[int], nums2: List[int]) -> int:\n",
    "        # ans = set(nums1) & set(nums2)\n",
    "        # return min(ans) if ans else -1\n",
    "\n",
    "        \n",
    "        return min(set(nums1) & set(nums2), default=-1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def getCommon(self, nums1: List[int], nums2: List[int]) -> int:\n",
    "        nums1.sort()\n",
    "        nums2.sort()\n",
    "        q=Counter(nums2)\n",
    "        for x in nums1:\n",
    "            if x in q:\n",
    "                return x\n",
    "        return -1    \n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def getCommon(self, nums1: List[int], nums2: List[int]) -> int:\n",
    "        t = set(nums1) & set(nums2)\n",
    "        if not t:\n",
    "            return -1\n",
    "        return min(t)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def getCommon(self, nums1: List[int], nums2: List[int]) -> int:\n",
    "        return min(set(nums1) & set(nums2), default=-1)\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def getCommon(self, nums1: List[int], nums2: List[int]) -> int:\n",
    "        nums3 = list(set(nums1) & set(nums2))\n",
    "        if nums3:\n",
    "            nums3.sort()\n",
    "            return nums3[0]\n",
    "        return -1\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def getCommon(self, nums1: List[int], nums2: List[int]) -> int:\n",
    "        s=set(nums1).intersection(set(nums2))\n",
    "        t=list(s)\n",
    "        if len(t)==0:\n",
    "            return -1\n",
    "        else:\n",
    "            return min(t)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def getCommon(self, nums1: List[int], nums2: List[int]) -> int:\n",
    "        s = set(nums1) & set(nums2)\n",
    "        if len(s) > 0:\n",
    "            return min(s)\n",
    "        else:\n",
    "            return -1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "\n",
    "class Solution:\n",
    "    def getCommon(self, nums1: List[int], nums2: List[int]) -> int:\n",
    "        l=list(set(nums1)&set(nums2))\n",
    "        l.sort()\n",
    "        if l:\n",
    "            return l[0]\n",
    "        return -1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def getCommon(self, nums1: List[int], nums2: List[int]) -> int:\n",
    "        return min(set(nums1) & set(nums2), default=-1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def getCommon(self, nums1: List[int], nums2: List[int]) -> int:\n",
    "        vis = set(nums1) & set(nums2)\n",
    "        if vis:\n",
    "            return min(vis)\n",
    "        return -1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def getCommon(self, nums1: List[int], nums2: List[int]) -> int:\n",
    "        return min(set(nums1) & set(nums2), default = -1)"
   ]
  }
 ],
 "metadata": {},
 "nbformat": 4,
 "nbformat_minor": 2
}
